package com.richrail.model.entities;

import com.richrail.state.WagonState;

public class Wagon implements Unit
{
	private String name;
	
	private int seatCount;
	
	private Train train;
	
	public Wagon(String name, int seatCount)
	{
		this.name = name;
		this.seatCount = seatCount;
	}
	
	@Override
	public String getName()
	{
		return name;
	}

	@Override
	public int getSeatCount()
	{
		return seatCount;
	}
	
	// We removed the getter because it was bad design, what is the point of encapsulation
	// if you make everything publicly available through getters?
	// Good article about the subject: http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html
	public boolean coupleToTrain(Train train)
	{
		// You cannot add a wagon to another train,
		// it needs to be removed from its current train first.
		if (train != null && this.train == null)
		{
			this.train = train;
			
			return true;
		}
		
		return false;
	}
	
	public Train decoupleFromTrain()
	{
		return train != null ? decoupleFromTrain(train) : null;
	}
	
	public Train decoupleFromTrain(Train train)
	{
		if (train == this.train)
		{
			this.train = null;
			
			return train;
		}
		
		return null;
	}
	
	public WagonState getState()
	{
		return new WagonState(name, seatCount);
	}
	
	@Override
	public boolean equals(Object o)
	{
		if (o instanceof Wagon)
		{
			return ((Wagon) o).name.equals(name);
		}
		
		return false;
	}
	
	@Override
	public String toString()
	{
		return name.toString();
	}
}
